package com.ewanghuiju.app.ui.main.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.base.SimpleActivity;
import com.ewanghuiju.app.model.prefs.ImplPreferencesHelper;
import com.ewanghuiju.app.test.bean.PintuanHomeBannerBean;
import com.ewanghuiju.app.test.bean.TestBean;
import com.ewanghuiju.app.ui.login.activity.LoginMainActivity;
import com.stx.xhb.androidx.XBanner;

import java.util.ArrayList;
import java.util.List;

import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/2/11
 */
public class GuideActivity extends SimpleActivity {

    @BindView(R.id.xbanner)
    XBanner xbanner;

    @Override
    protected int getLayout() {
        return R.layout.activity_guide;
    }

    @Override
    protected void initEventAndData() {
        finishAnim = false;
        initBanner();
    }

    /**
     * 加载banner
     */
    boolean isScrolled;

    private int mPosition;

    public void initBanner() {
        //测试数据  测试bean
        List<PintuanHomeBannerBean> bannerList = new ArrayList<>();
        PintuanHomeBannerBean bean = new PintuanHomeBannerBean(TestBean.mUrl);
        bean.setResId(R.mipmap.guide1);
        bannerList.add(bean);
        bean = new PintuanHomeBannerBean(TestBean.mUrl);
        bean.setResId(R.mipmap.guide2);
        bannerList.add(bean);
        bean = new PintuanHomeBannerBean(TestBean.mUrl);
        bean.setResId(R.mipmap.guide3);
        bannerList.add(bean);
        bean = new PintuanHomeBannerBean(TestBean.mUrl);
        bean.setResId(R.mipmap.guide4);
        bannerList.add(bean);
        xbanner.setBannerData(bannerList);
        //加载广告图片
        xbanner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
//                ImageLoader.load(mContext, bannerList.get(position).getResId(), (ImageView) view);
                Glide.with(mContext).load(bannerList.get(position).getResId()).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).into((ImageView) view);
            }
        });
        xbanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mPosition = position;
//                if (position == 3) {
//                    new Handler().postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            ImplPreferencesHelper mImplPreferencesHelper = App.getAppComponent().preferencesHelper();
//                            mImplPreferencesHelper.setIsFirst(true);
//                            startActivity(new Intent(mContext, MainActivity.class));
////                            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
//                            overridePendingTransition(R.anim.scale_down, R.anim.scale_up);
//                            onBackPressedSupport();
//                        }
//                    }, 1000);
//                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {


                switch (state) {

                    case ViewPager.SCROLL_STATE_DRAGGING:

                        isScrolled = false;

                        break;

                    case ViewPager.SCROLL_STATE_SETTLING:

                        isScrolled = true;

                        break;

                    case ViewPager.SCROLL_STATE_IDLE:

                        if (mPosition == 3 && !isScrolled) {
                            //滑到最后一个页面之后的继续操作
                            toLoginMain();
                        }
                        isScrolled = true;

                        break;

                }

            }
        });
        xbanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, Object model, View view, int position) {
                if (position == 3) {
                    toLoginMain();
                }
            }
        });
    }

    public void toLoginMain() {
        ImplPreferencesHelper mImplPreferencesHelper = App.getAppComponent().preferencesHelper();
        mImplPreferencesHelper.setIsFirst(true);
        startActivity(new Intent(mContext, LoginMainActivity.class));
//                            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        overridePendingTransition(R.anim.scale_down, R.anim.scale_up);
        onBackPressedSupport();
    }

    @Override
    protected void initImmersionBar() {
//        super.initImmersionBar();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
